using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class readImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		string productID = Request.QueryString["ProductID"].ToString();
		string photoSize = Request.QueryString["Size"].ToString();
		System.Data.SqlClient.SqlDataReader imageReader;
		string dbConn = ConfigurationManager.ConnectionStrings["AdvWorks"].ConnectionString;
		System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(dbConn);
		sqlConn.Open();
		System.Data.SqlClient.SqlCommand imageCommand = new System.Data.SqlClient.SqlCommand(
			"SELECT ThumbNailPhoto, LargePhoto FROM Production.Product "
				+ "INNER JOIN Production.ProductProductPhoto ON Production.Product.ProductID = Production.ProductProductPhoto.ProductID "
				+ "INNER JOIN Production.ProductPhoto ON Production.ProductProductPhoto.ProductPhotoID = Production.ProductPhoto.ProductPhotoID "
				+ "WHERE Production.Product.ProductID=" + productID,sqlConn);
		imageReader = imageCommand.ExecuteReader(CommandBehavior.CloseConnection);
		imageReader.Read();
		if (photoSize == "ThumbNail")
		{
			byte[] imageBytes = (byte[])imageReader["ThumbNailPhoto"];
			Response.ContentType = "image/GIF";
			Response.BinaryWrite(imageBytes);
		}
		else if (photoSize == "Large")
		{
			byte[] imageBytes = (byte[])imageReader["LargePhoto"];
			Response.ContentType = "image/JPEG";
			Response.BinaryWrite(imageBytes);
		}
		
    }
}
